package com.orange.expireFile.format;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.orange.expireFile.entity.ExpireFile;

public class ExpireFileFormat {

	private static final Logger logger = Logger.getLogger(ExpireFileFormat.class);

	public static Map<String, ExpireFile> expireFileMap = new HashMap<String, ExpireFile>();

	/**
	 * @Description: 加载过期文件配置文档，进行格式化生成map
	 * @param xmlPath
	 * @return
	 * @throws Exception
	 */
	public static Map<String, ExpireFile> getExpireFileMapByXml(String xmlPath) throws Exception {

		if (expireFileMap == null) {
			throw new NullPointerException("checkExpireFileMap(Map<String, ExpireFile>)");
		}

		expireFileMap.clear();

		SAXReader saxReader = new SAXReader();
		FileInputStream in = new FileInputStream(new File(xmlPath));
		Document document = saxReader.read(in);
		// 获取xml节点信息
		List<Node> nodes = document.selectNodes("/expireFileInfos/expireFileInfo");
		for (Iterator<Node> it = nodes.iterator(); it.hasNext();) {
			Element elm = (Element) it.next(); // 获取expireFileInfo节点
			Element eleExpireFileType = elm.element("expireFileType"); // 获取expireFileInfo节点下的expireFileType元素
			String expireFileType = eleExpireFileType.getText();
			String expireFilePath = elm.element("expireFilePath").getText();
			Integer expireFileTime = Integer.valueOf(elm.element("expireFileTime").getText());
			String expireFileScheduler = elm.element("expireFileScheduler").getText();
			Integer expireFileSchedulerStatus = Integer.valueOf(elm.element("expireFileSchedulerStatus").getText());
			String expireFileDes = elm.element("expireFileDes").getText();

			ExpireFile expireFile = new ExpireFile(expireFileType, expireFilePath, expireFileTime, expireFileScheduler,
					expireFileSchedulerStatus, expireFileDes);
			logger.info(expireFile.toString());
			expireFileMap.put(expireFileType, expireFile);
		}

		return expireFileMap;
	}

	public static Map<String, ExpireFile> getExpireFileMap() {
		return expireFileMap;
	}
}
